package deleteface.bank.jqsoft.cn.deleteface.http.presenter;

import android.content.Context;

import com.google.gson.Gson;


import com.trello.rxlifecycle.ActivityEvent;


import javax.inject.Inject;

import deleteface.bank.jqsoft.cn.deleteface.base.AbstractActivity;

import deleteface.bank.jqsoft.cn.deleteface.http.BasePresenter;
import deleteface.bank.jqsoft.cn.deleteface.http.contract.HeadCollectContract;
import deleteface.bank.jqsoft.cn.deleteface.http.contract.HttpResultBaseGWBean;
import deleteface.bank.jqsoft.cn.deleteface.setip.Util;
import deleteface.bank.jqsoft.cn.deleteface.webservice.RequestManager;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * mars
 */
public class HeadCollectPresenter extends BasePresenter<HeadCollectContract.View> implements HeadCollectContract.presenter {

    private final HeadCollectContract.View view;


    @Inject
    public HeadCollectPresenter(HeadCollectContract.View view) {
        this.view = view;
    }

    public void runNote(String body,String url) {
        final Context context = (Context) view;
        AbstractActivity abstractActivity = (AbstractActivity) context;
        RequestManager.getInstance(url)
                .mServiceStore
                .applyDelete(body)
//                .compose(abstractActivity.<ResponseBody>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .compose(abstractActivity.<ResponseBody>bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Util.showGifProgressDialog(context);

                    }

                    @Override
                    public void onCompleted() {
                        Util.hideGifProgressDialog(context);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Util.hideGifProgressDialog(context);
                        view.onDeleteFailure(e.getMessage());

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {

                        String json = Util.DecodeXmlJosn(responseBody);
                        HttpResultBaseGWBean result = new Gson().fromJson(json, HttpResultBaseGWBean.class);
                        if (result != null) {
                            if (result.getsSuccess().equals("1")) {
                                view.onDeletFaceSuccess(result);
                            } else if (result.getsSuccess().equals("0")) {
                                String msg = result.getsMessage();
                                view.onDeleteFailure(msg);
                            }
                        }
                    }

                });

    }


    public void deleteWPReadCardInfo(String body,String url) {
        final Context context = (Context) view;
        AbstractActivity abstractActivity = (AbstractActivity) context;
        RequestManager.getInstance(url)
                .mServiceStore
                .applyWPReadCardInfo(body)
//                .compose(abstractActivity.<ResponseBody>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .compose(abstractActivity.<ResponseBody>bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Util.showGifProgressDialog(context);

                    }

                    @Override
                    public void onCompleted() {
                        Util.hideGifProgressDialog(context);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Util.hideGifProgressDialog(context);
                        view.onDeleteFailure(e.getMessage());

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {

                        String json = Util.DecodeXmlJosn(responseBody);
                        HttpResultBaseGWBean result = new Gson().fromJson(json, HttpResultBaseGWBean.class);
                        if (result != null) {
                            if (result.getsSuccess().equals("1")) {
                                view.onDeletFaceSuccess(result);
                            } else if (result.getsSuccess().equals("0")) {
                                String msg = result.getsMessage();
                                view.onDeleteFailure(msg);
                            }
                        }
                    }

                });

    }
}